package utils

import "fmt"

type FamilyAccount struct{
	key string
	loop bool
	balance float64
	money float64
	note string
	details string
	flag bool
}
func (this *FamilyAccount) MainMenu(){
	for{
		fmt.Println("------------请输入家庭收支明细------------")
		fmt.Println("1 收支明细")
		fmt.Println("2 登记收入")
		fmt.Println("3 登记支出")
		fmt.Println("4 退    出")
		fmt.Println("请输入你的选择")
		fmt.Scanln(&this.key)
		switch this.key{
		case "1":
			this.showDetails()
		case "2":
			this.income()
		case "3":
			this.pay()
		case "4":
			this.out()
		default:
			fmt.Println("当前输入有误 请重新输入")
		}
		if !this.loop{
			break
		}
	}
}
func (this *FamilyAccount) out(){
	scoic:=""
	fmt.Println("y/n")
	fmt.Scanln(&scoic)
	if scoic=="y"{ this.loop=false }
	if scoic!="y"&&scoic!="n"{
		fmt.Println("输入有误 请从新输入")
		this.out()
	}
}

func (this *FamilyAccount) pay(){
	fmt.Println("输入支出的钱")
	fmt.Scanln(&this.money)
	if this.money>this.balance{
		fmt.Println("余额不足")
	}else{
		this.balance-=this.money
		fmt.Println("输入支出的钱的说明")
		fmt.Scanln(&this.note)
		this.details+=fmt.Sprintf("\n支出\t%v\t\t%v\t%v",this.balance,this.money,this.note)
		this.flag=false
		this.showDetails()
	}
}

func (this *FamilyAccount) income(){
	fmt.Println("输入收入的钱")
	fmt.Scanln(&this.money)
		fmt.Println("输入收入的钱的说明")
		this.balance+=this.money
		fmt.Scanln(&this.note)
		this.details+=fmt.Sprintf("\n支出\t%v\t\t%v\t%v",this.balance,this.money,this.note)
		this.flag=false
		this.showDetails()
}
func (this *FamilyAccount) showDetails(){
	if this.flag{
		fmt.Println("当前没有收支明细 请去支出一条吧")
	}else{
		fmt.Println(this.details)
	}
}
func NewFamilyAccount() (*FamilyAccount){
	return &FamilyAccount{
		key :"",
		loop :true,
		balance :10000.0,
		money :0.0,
		note :"",
		details :"收支\t账户金额\t收支明细\t说明",
		flag :true,
	}
}